

package com.mt.mall.controller.merchant;

import org.springframework.security.access.prepost.PreAuthorize;
import com.mt.common.core.annotation.ApiPageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.mt.common.core.web.base.PageDTO;
import com.mt.common.core.web.base.PageResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.mt.mall.dto.merchant.ShopEditDto;
import com.mt.mall.entity.merchant.Shop;
import com.mt.mall.service.merchant.ShopService;
import com.mt.common.system.service.UserService;


@Api(tags = "店铺")
@RestController
@RequestMapping("/api/mall/merchant/Shop")
@CrossOrigin(allowCredentials = "true")
public class ShopController {
    private static Logger logger = LoggerFactory.getLogger(ShopController.class);


    @Autowired
    private ShopService shopService;
    @Autowired
    private UserService userService;

    /**
     * 根据分页参数查询店铺集合
     *
     * @param pageDTO 分页条件
     */
    @PreAuthorize("hasAuthority('mall:merchant:Shop:view')")
    @ApiOperation("分页查询店铺")
    @ApiPageParam
    @PostMapping("/findShops")
    public PageResultDTO findShops(@RequestBody PageDTO pageDTO) {
        return this.shopService.findShops(pageDTO);
    }

    /**
     * 根据ID查询指定的店铺
     *
     * @param shopId Id
     */
    @PreAuthorize("hasAuthority('mall:merchant:Shop:view')")
    @ApiOperation("根据id查询店铺")
    @ApiPageParam
    @PostMapping("/findShop")
    public Shop findShop(@RequestParam Long shopId) {
        return this.shopService.findShop(shopId);
    }

    /**
     * 根据ID查询指定的店铺(包含外键名称)
     *
     * @param shopId Id
     */
    @PreAuthorize("hasAuthority('mall:merchant:Shop:view')")
    @ApiOperation("根据ID查询指定的店铺(包含外键名称)")
    @PostMapping("/findShopForView")
    public Shop findShopForView(@RequestParam Long shopId) {
        return this.shopService.findShopWithForeignName(shopId);
    }

    /**
     * 根据ID查询指定的店铺(包含店铺和外键名称)
     *
     * @param shopId Id
     */
    @PreAuthorize("hasAuthority('mall:merchant:Shop:view')")
    @ApiOperation("根据ID查询指定的店铺(包含店铺和外键名称)")
    @PostMapping("/findShopForEdit")
    public ShopEditDto findShopForEdit(@RequestParam Long shopId) {
        ShopEditDto shopEditDto = new ShopEditDto();
        shopEditDto.setShop(this.shopService.findShopWithForeignName(shopId));

        this.prepareShopEditDto(shopEditDto);

        return shopEditDto;
    }

    /**
     * 根据ID查询指定的店铺(只提取ID 和 Name)
     *
     * @param shopId Id
     */
    @PreAuthorize("hasAuthority('mall:merchant:Shop:view')")
    @ApiOperation("根据ID查询指定的店铺(只提取ID 和 Name)")
    @PostMapping("/findShopsWithIdNameById")
    public Shop findShopsWithIdNameById(@RequestParam Long shopId) {
        return this.shopService.findShopsWithIdNameById(shopId);
    }

    /**
     * 根据名称查询店铺集合(只提取ID 和 Name)
     *
     * @param shopName 名称
     */
    @PreAuthorize("hasAuthority('mall:merchant:Shop:view')")
    @ApiOperation("根据名称查询店铺集合(只提取ID 和 Name)")
    @PostMapping("/findShopsWithIdNameByName")
    public List<Shop> findShopsWithIdNameByName(@RequestParam String shopName) {
        return this.shopService.findShopsWithIdNameByName(shopName);
    }


    /**
     * 创建新的店铺
     */
    @PreAuthorize("hasAuthority('mall:merchant:Shop:add')")
    @ApiOperation("创建新的店铺")
    @PostMapping("/createShop")
    public ShopEditDto createShop() {
        ShopEditDto shopEditDto = new ShopEditDto();
        shopEditDto.setShop(new Shop());

        this.prepareShopEditDto(shopEditDto);
        return shopEditDto;
    }

    private void prepareShopEditDto(ShopEditDto shopEditDto) {
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        shopEditDto.setUserUsers(this.userService.findAllUsersWithIdName());
    }

    /**
     * 新增保存店铺
     *
     * @param shop 实体对象
     */
    @PreAuthorize("hasAuthority('mall:merchant:Shop:add')")
    @ApiOperation("新增保存店铺")
    @PostMapping("/saveShop")
    public Shop saveShop(@RequestBody Shop shop) {
        return this.shopService.saveShop(shop);
    }

    /**
     * 修改保存店铺
     *
     * @param shop 实体对象
     */
    @PreAuthorize("hasAuthority('mall:merchant:Shop:update')")
    @ApiOperation("修改保存店铺")
    @PostMapping("/updateShop")
    public Shop updateShop(@RequestBody Shop shop) {
        return this.shopService.updateShop(shop);
    }

    /**
     * 根据ID删除店铺
     *
     * @param shopId ID
     */
    @PreAuthorize("hasAuthority('mall:merchant:Shop:remove')")
    @ApiOperation("根据ID删除店铺")
    @PostMapping("/deleteShop")
    public void deleteShop(@RequestParam Long shopId) {
        this.shopService.deleteShop(shopId);
    }


}

